﻿using ARSoft.Tools.Net;
using ARSoft.Tools.Net.Dns;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.NetworkInformation;

namespace HostsTool.Currency
{
    internal class DnsHelper
    {
        /// <summary>
        /// 第三方嗅探DNS
        /// </summary>
        /// <param name="DnsIP"></param>
        /// <param name="Hosts"></param>
        /// <returns></returns>
        public List<string> GetHostIp(string DnsIP, string HostUrl)
        {
            List<string> HostList = new List<string>();
            try
            {
                DnsClient dnsClient = new DnsClient(IPAddress.Parse(DnsIP), 1000);

                if (HostUrl.IndexOf("http") >= 0)
                {
                    HostUrl = new Uri(HostUrl).Host;
                }
                DnsMessage dnsMessage = dnsClient.Resolve(DomainName.Parse(HostUrl), RecordType.A);
                foreach (DnsRecordBase dnsRecord in dnsMessage.AnswerRecords)
                {
                    ARecord aRecord = dnsRecord as ARecord;
                    if (aRecord == null)
                        continue;
                    Console.WriteLine("第三方探测DNS{0}，得到的解析结果{1}。共{2}个结果", DnsIP, aRecord.Address.ToString(), dnsMessage.AnswerRecords.Count);

                    HostList.Add(aRecord.Address.ToString());
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("---------------");
                Console.WriteLine(HostUrl);
                Console.WriteLine("---------------");
                PrintLog.Log(ex);
            }

            return HostList;
        }

        /// <summary>
        /// 本地嗅探DNS
        /// </summary>
        /// <param name="Hosts"></param>
        public List<string> GetHostIpByLocal(string Hosts)
        {
            List<string> HostList = new List<string>();
            try
            {
                IPAddress[] myIp = Dns.GetHostAddresses(new Uri(Hosts).Host);
                foreach (IPAddress ips in myIp)
                {
                    Console.WriteLine("本地探测DNS，得到的解析结果{0}。共{1}个结果", ips.ToString(), myIp.Length);

                    HostList.Add(ips.ToString());
                }
            }
            catch (Exception ex) { PrintLog.Log(ex); }

            return HostList;
        }

        /// <summary>
        /// 获取本地连接的所有信息
        /// </summary>
        /// <returns></returns>
        public IPInterfaceProperties GetLocalDns()
        {
            NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface adapter in nics)
            {
                bool Pd1 = (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet); //判断是否是以太网连接
                if (true)
                {
                    IPInterfaceProperties ip = adapter.GetIPProperties();     //IP配置信息
                    if (ip.UnicastAddresses.Count > 0)
                    {
                        Console.WriteLine("IP地址" + ip.UnicastAddresses[0].Address.ToString());
                        Console.WriteLine("子网掩码" + ip.UnicastAddresses[0].IPv4Mask.ToString());
                    }
                    if (ip.GatewayAddresses.Count > 0)
                    {
                        Console.WriteLine("网关" + ip.GatewayAddresses[0].Address.ToString());
                    }
                    int DnsCount = ip.DnsAddresses.Count;
                    Console.WriteLine("DNS服务器地址：");
                    if (DnsCount > 0)
                    {
                        try
                        {
                            Console.WriteLine("Dns" + ip.DnsAddresses[0].ToString());
                            Console.WriteLine("Dns" + ip.DnsAddresses[1].ToString());
                        }
                        catch (Exception er)
                        {
                            throw er;
                        }
                    }
                    return ip;
                }
            }
            return null;
        }
    }
}